<?php
declare (strict_types=1);

namespace app\exam\model;

use app\common\model\{BaseModel, ProductType};
use think\model\concern\SoftDelete;

/**
 * 套卷模型
 */
class Rollup extends BaseModel
{
    use softDelete;
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'exam_module_rollup';

    /**
     * 定义隐藏字段
     * @var [type]
     */
    protected $hidden = [ 'create_time', 'update_time', 'delete_time', 'status' ];

    /**
     * 全局查询范围
     */
    protected $globalScope = ['status'];
    
    /**
     * 添加器
     * @var array
     */
    protected $append = ['user_price']; // 根据用户身份计算的价格

    /**
     * 限制禁用条件
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-03-30
     * @param  [type] $query [description]
     * @return [type]        [description]
     */
    public function scopeStatus($query)
    {
        // 禁用状态
        $query->where('status', 1);
    }

    /**
     * 根据用户身份返回价格
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date     21/03/29
     * @param string $value
     * @param $data
     * @return float
     */
    public function getUserPriceAttr($value = '', $data): float
    {
        return self::getUserPrice($data);
    }

    /**
     * 套卷试卷关联
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-03-30
     */
    public function rollupPaper()
    {
        return $this->hasMany(RollupPaper::class, 'rollup_id')->hidden(['id', 'rollup_id', 'create_time', 'update_time']);
    }

    /**
     * 套卷试卷关联
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-03-30
     */
    public function firstPaperId()
    {
        return $this->hasOne(RollupPaper::class, 'rollup_id')->bind(['first_paper_id' => 'paper_id']);
    }

    /**
     * 套卷是否购买
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-03-30
     */
    public function hasBought()
    {
        return $this->hasOne(OrderItem::class, 'product_id')
        ->whereProductTypeId(ProductType::getProductIdByKey('rollup'))
        ->join('payment_order po', "po.order_no=" . env('database.prefix', '') . "payment_order_item.order_no AND po.user_id=" . request()->mid )
        ->where('status', 'IN', '20,30,40');
    }

    /**
     * 购买人数
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-03-31
     * @return [type] [description]
     */
    public function boughtNum()
    {
        return $this
        ->hasMany(OrderItem::class, 'product_id')
        ->whereProductTypeId(ProductType::getProductIdByKey('rollup'))
        ->join('payment_order po', "po.order_no=".env('database.prefix', '')."payment_order_item.order_no")
        ->where('status', 'IN', '20,30,40');
    }

    /**
     * 练习人数
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-05-08
     * @return [type] [description]
     */
    public function practiceNum()
    {
        return $this->hasMany(PaperAnswer::class, 'rollup_id');
    }

    /**
     * 关联机构
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-05-08
     * @return [type] [description]
     */
    public function mhmId()
    {
        return $this->belongsTo(Module::class, 'module_id')->bind(['mhm_id']);
    }

}